package com.kawa.kemanage.controller;

import com.kawa.kemanage.entity.User;
import com.kawa.kemanage.util.Result;
import com.kawa.kemanage.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 登录功能使用
     * @param username 前端传入的用户名
     * @param password 前端传入的密码
     * @return 返回json格式的token以及状态码
     */
    @PostMapping("/login")
    public Result<Map<String, Object>> login(@RequestParam String username, @RequestParam String password) {
        try {
             //本来可以使用try-catch的,老懒批了
            Map<String, Object> loginInfo = userService.login(username, password);
            return Result.success(loginInfo);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 根据名字和用户类型查询所有用户
     * @return
     */
    @GetMapping("/list")
    public Result<Map<String, Object>> info(
            @RequestParam(required = true,defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize ,
            @RequestParam(required = false) String userName,
            @RequestParam(required = false) Integer userType
    ) {
        try {
            Result<Map<String, Object>> result = userService.findAllUsers(pageNum, pageSize, userName, userType);
            return result;
        } catch (Exception e) {
            return Result.error(e.getMessage());

        }
    }

    /**
     * 根据id查询用户
     * @param id
     * @return
     */

    @GetMapping("/info/{id}")
    public Result<Map<String, Object>> findUserById(@PathVariable Integer id) {
        try {
            User user = userService.findUserById(id);
            Map<String, Object> data = new HashMap<>();
            data.put("user", user);
            return Result.success(data);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 修改用户
     * @param user
     * @param id
     * @return
     */
    @PostMapping("/update/{id}")
    public Result<Map<String, Object>> updateUser(@PathVariable Integer id, @RequestBody User user) {
        try {
            if (user == null) {
                return Result.error("用户信息不能为空");
            }
            user.setId(id);
            int result = userService.updateUserById(user);
            if (result > 0) {
                return Result.success();
            }else {
                return Result.error("修改失败");
            }
        }
        catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 根据id删除用户
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public Result<Map<String, Object>> delete(@PathVariable Integer id) {
        try {
            int result = userService.deleteUserById(id);
            if (result > 0) {
                return Result.success();
            }else {
                return Result.error("删除失败");
            }
        }catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }



    /**
     * 新增用户
     * @param user
     * @return
     */
    @PostMapping("/add")
    public Result<Map<String, Object>> addUser(@RequestBody User user) {
        try {
            int result = userService.insertUser(user);
            if (result > 0) {
                return Result.success();
            }else {
                return Result.error("新增失败");
            }
        }catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }



    /**
         * 这是测试方法，不知道怎么返回结果的可以看一下怎么返回
         * @return
         */

        @GetMapping("/test")
        public Result<Map<String, Object>> test () {
            Map<String, Object> data = new HashMap<>();
            data.put("name", "张三");
            data.put("age", 25);
            return Result.success(data);
        }
    }

